﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using FreeTextBoxControls;
using System.Drawing;
using LTP.Accounts.Bus;


namespace TCS.Web.Admin.NewsManage
{
    public partial class releasenot : System.Web.UI.Page
    {
        private int PageIndex
        {
            get { return (int)ViewState["PageIndex"]; }
            set { ViewState["PageIndex"] = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                grid.BorderColor = ColorTranslator.FromHtml(Application[Session["Style"].ToString() + "xtable_bordercolorlight"].ToString());
                grid.HeaderStyle.BackColor = ColorTranslator.FromHtml(Application[Session["Style"].ToString() + "xtable_titlebgcolor"].ToString());

                ImgConfirm.Attributes.Add("onclick", "return confirm('删除该广告将随同删除相应的显示记录和网址关联信息！\\n你是否确定删除这些记录？');");
                if (Request.Params["PageIndex"] != null && Request.Params["PageIndex"].ToString() != "")
                {
                    Session["pageNewsr"] = Convert.ToInt32(Request.Params["PageIndex"]);
                    PageIndex = Convert.ToInt32(Request.Params["PageIndex"]);
                }
                else
                {
                    if (Session["pageNewsr"] != null && Session["pageNewsr"].ToString() != "")
                    {
                        PageIndex = Convert.ToInt32(Session["pageNewsr"]);
                    }
                    else
                    {
                        PageIndex =0;
                        Session["pageNewsr"] = PageIndex;
                    }
                }

                dataBind(PageIndex);
                BindInfoType();
            }
        }
        private void BindInfoType()
        {
            TCS.BLL.NewsManage.NewsClass bll = new TCS.BLL.NewsManage.NewsClass();
            DataSet ds = bll.GetList("");
            this.dropInfoType.DataSource = ds;
            this.dropInfoType.DataTextField = "ClassDesc";
            this.dropInfoType.DataValueField = "CataId";
            this.dropInfoType.DataBind();
            dropInfoType.Items.Add(new ListItem("请选择分类", ""));
            dropInfoType.Items.FindByText("请选择分类").Selected = true;
        }
        /// <summary>
        /// 根据当前用户所属的区域
        /// 绑定需要审核的信息
        /// zxq
        /// </summary>
        /// <param name="pageIndex"></param>
        private void dataBind(int PageIndex)
        {
           
            LTP.Accounts.Bus.User currentUser = (LTP.Accounts.Bus.User)Session["UserInfo"];
            TCS.Model.Gsp_Administrative Class = new TCS.BLL.Gsp_Administrative().GetModel(int.Parse(currentUser.DepartmentID));
            int depth = Class.Depth;//流程

           

            TCS.BLL.NewsManage.News bll = new TCS.BLL.NewsManage.News();
            string strWhere = " ";
            //string sq = " and depth = " + depth;
            //strWhere += "frequency ="+currentUser.DepartmentID+sq;
            //strWhere += " or frequency in(select addid from CInfo_Administrative where parentid="+currentUser.DepartmentID+" )" + sq;
            strWhere = " IsTop !=1 and depth = " + depth;
            if (Session["strWhereNewsRelea"] != null && Session["strWhereNewsRelea"].ToString() != "")
            {
                strWhere = Session["strWhereNewsRelea"].ToString();
            }
            DataSet ds = new DataSet();
            ds = bll.GetList(strWhere);
            grid.DataSource = ds.Tables[0].DefaultView;
            int record_Count = ds.Tables[0].Rows.Count;
            int page_Size = grid.PageSize;
            int totalPages = int.Parse(Math.Ceiling((double)record_Count / page_Size).ToString());
            //if (totalPages > 0)
            //{
            //    if ((PageIndex + 1) > totalPages)
            //        PageIndex = totalPages - 1;
            //}
            //else
            //{
            //    PageIndex = 0;
            //}
            grid.CurrentPageIndex = PageIndex;
            grid.DataBind();
            //int page_Count = grid.PageCount;
            //int page_Current = PageIndex + 1;
            if (PageIndex == 0)
            {
                btnFirst.Enabled = false;
                btnPrev.Enabled = false;
                if (grid.PageCount-1 == PageIndex)
                {
                    btnLast.Enabled = false;
                    btnNext.Enabled = false;
                }
            }
            else if (PageIndex == grid.PageCount - 1)
            {
                btnLast.Enabled = false;
                btnNext.Enabled = false;
            }
           
            lblCurrentPage.Text = (PageIndex + 1).ToString();
            lblPageCount.Text = totalPages.ToString();
            lblRowsCount.Text = record_Count.ToString();
        }
        protected void btn_Search_Click(object sender, ImageClickEventArgs e)
        {
            LTP.Accounts.Bus.User currentUser = (LTP.Accounts.Bus.User)Session["UserInfo"];
            TCS.Model.Gsp_Administrative Class = new TCS.BLL.Gsp_Administrative().GetModel(int.Parse(currentUser.DepartmentID));
            int depth = Class.Depth;//流程

            TCS.BLL.NewsManage.News bll = new TCS.BLL.NewsManage.News();
            string strsql = "";
            string strWhere = "";
            strWhere = " IsTop !=1 and depth = " + depth;
            string adname = this.txtKey.Text.Trim();
            string field = this.DropField.SelectedValue;
            if (adname != "" && adname != "请输入关键字")
            {
                strsql += " and (" + field + " like'%" + adname + "%')";
            }
            if (dropInfoType.SelectedValue != "")
            {
                strsql += " and ClassId=" + dropInfoType.SelectedValue;
            }
            if (strsql != "")
            {
                Session["strWhereNewsRelea"] = strWhere + strsql;
            }
            else
            {
                Session["strWhereNewsRelea"] = strWhere;
            }
            DataSet ds = new DataSet();
            ds = bll.GetList(Session["strWhereNewsRelea"].ToString());
            grid.DataSource = ds.Tables[0].DefaultView;
            Response.Redirect("releasenot.aspx?page=1");
        }
        protected void btn_Relese_Click(object sender, EventArgs e)
        {
            TCS.BLL.NewsManage.News bll = new TCS.BLL.NewsManage.News();
            string idlist = GetSelIDlist();
            bll.ReleaseList(idlist);
            Response.Redirect("Release.aspx");

        }
        protected void btn_NoRelease_Click(object sender, EventArgs e)
        {
           

        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        //protected void Confirm_Click(object sender, EventArgs e)
        //{
        //    TCS.BLL.NewsManage.News bll = new TCS.BLL.NewsManage.News();
        //    string idlist = GetSelIDlist();
        //    if (idlist.Length > 0)
        //        bll.DeleteList(idlist);
        //    else
        //    {

        //    }
        //    Response.Redirect("Release.aspx");
        //}
        /// <summary>
        /// 判断checkbox
        /// </summary>
        /// <returns></returns>
        private string GetSelIDlist()
        {
            string dgIDs = "";
            bool BxsChkd = false;
            foreach (DataGridItem item in grid.Items)
            {
                CheckBox deleteChkBxItem = (CheckBox)item.FindControl("SelectThis");
                if (deleteChkBxItem.Checked)
                {
                    BxsChkd = true;
                    dgIDs += item.Cells[1].Text + ",";
                }
            }
            if (BxsChkd)
            {
                dgIDs = dgIDs.Substring(0, dgIDs.LastIndexOf(","));
            }
            return dgIDs;
        }
        protected void grid_ItemDataBound(object sender, DataGridItemEventArgs e)
        {

            if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
            {
                string Dormancy = (string)DataBinder.Eval(e.Item.DataItem, "Dormancy");
                int ClassId = (int)DataBinder.Eval(e.Item.DataItem, "ClassId");
                int priority = (int)DataBinder.Eval(e.Item.DataItem, "priority");
                int istop = (int)DataBinder.Eval(e.Item.DataItem, "istop");

                if (Dormancy.ToLower() == "1")
                {
                    e.Item.Cells[5].Text = "<span style=\" color:Red\">网页</span>";
                }
                else
                {
                    e.Item.Cells[5].Text = "<span style=\" color:Green\">短信</span>";
                }

                TCS.BLL.NewsManage.NewsClass bll = new TCS.BLL.NewsManage.NewsClass();
                TCS.Model.NewsManage.NewsClass mod = bll.GetModelByCache(ClassId);
                if (mod != null)
                {
                    e.Item.Cells[6].Text = mod.ClassDesc;
                }
                else
                {
                    e.Item.Cells[6].Text = "未知类别";
                }
                if (priority == 0)
                {
                    e.Item.Cells[8].Text = "<span style=\" color:Red\">不发布</span>";
                }
                else if (priority == 1)
                {
                    e.Item.Cells[8].Text = "<span style=\" color:Green\">全县</span>";
                }
                else if (priority == 2)
                {
                    e.Item.Cells[8].Text = "<span style=\" color:Green\">全市</span>";
                }
                else
                {
                    e.Item.Cells[8].Text = "<span style=\" color:Green\">全省</span>";
                }

                if (istop == 0 )
                {
                    e.Item.Cells[9].Text = "<span style=\" color:Red\">未审核</span>";
                }

                if (istop==2 )
                {
                    e.Item.Cells[9].Text = "<span style=\" color:Gray\">审核中</span>";
                }
                if (istop==1 )
                {
                    e.Item.Cells[9].Text = "<span style=\" color:Green\">已审核</span>";
                }
                //else if (istop == 1)
                //{
                //    e.Item.Cells[9].Text = "<span style=\" color:Green\">全县</span>";
                //}
                //else if (istop == 2)
                //{
                //    e.Item.Cells[9].Text = "<span style=\" color:Green\">全市</span>";
                //}
                //else
                //{
                //    e.Item.Cells[9].Text = "<span style=\" color:Green\">已审核</span>";

                //}

            }
            //if (e.Row.RowType == DataControlRowType.DataRow)
            //{
            //    string title = Application[Session["Style"].ToString() + "xtable_titlebgcolor"].ToString();
            //    //string bgcolor = Application[Session["Style"].ToString() + "xtable_bgcolor"].ToString();
            //    e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='" + title + "';this.style.cursor='hand';");
            //    //当鼠标移走时还原该行的背景色
            //    e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor");
            //}
            if (e.Item.ItemIndex != -1)
            {
                string title = Application[Session["Style"].ToString() + "xtable_titlebgcolor"].ToString();
                e.Item.Attributes.Add("onmouseover ", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='" + title + "';this.style.cursor='hand';");
                e.Item.Attributes.Add("onmouseout ", "this.style.backgroundColor=currentcolor");
            }
        }

        protected void grid_ItemCommand(object source, DataGridCommandEventArgs e)
        {
            LTP.Accounts.Bus.User currUser = (LTP.Accounts.Bus.User)Session["UserInfo"];
            TCS.Model.Gsp_Administrative Class = new TCS.BLL.Gsp_Administrative().GetModel(int.Parse(currUser.DepartmentID));
            int currDepth = Class.Depth;//当前用户所在的深度

            if (e.CommandName == "Delete")
            {
                int NewsID = (int)grid.DataKeys[e.Item.ItemIndex];
                TCS.BLL.NewsManage.News bll = new TCS.BLL.NewsManage.News();
                bll.Delete(NewsID);
                dataBind(grid.CurrentPageIndex + 1);
            }
            if (e.CommandName == "shenhe")
            {

                int tip = int.Parse(e.CommandArgument.ToString());
                int NewsID = (int)grid.DataKeys[e.Item.ItemIndex];
                TCS.BLL.NewsManage.News bll = new TCS.BLL.NewsManage.News();
                TCS.Model.NewsManage.News model = bll.GetModel(NewsID);  //当前信息
               /***********zxq2*************/
                TCS.Model.Gsp_Administrative administrative = new TCS.BLL.Gsp_Administrative().GetModel(model.Frequency);

                TCS.Model.CInfo_FlowManage flowManage = new TCS.BLL.CInfo_FlowManage().GetModel(administrative.Depth);
                string[] str = flowManage.FLOWROUTE.Split(',');
                int seat = getIndex(str, model.Depth);  //当前用户所对应所有审核中的位置  数组中的位置
                if (flowManage.FLOWROUTE.Contains(currDepth.ToString())) //有自己审核的信息
                {

                    if(str.Length==seat)              //相等表示审核的次数和当前的深度相同 即最后一个审核人
                    {
                        bll.ShenheList(NewsID.ToString(), 0, Convert.ToInt32(str[seat-1]), 1, 0); //审核信息   最终审核通过
                    }
                    if(str.Length>seat)              //表示当前的审核深度还没有达到最终的审核次数 即 还可以向上提交审核
                    {
                        bll.ShenheList(NewsID.ToString(), 0, Convert.ToInt32(str[seat]), 2, 0); //审核信息  (ˇˍˇ） 向上级提交审核  
                    }
                    //if (str.Length ==1 || int.Parse(str[str.Length-1])==currDepth)  //自己不是最后的审核人
                    //{
                    //    bll.ShenheList(NewsID.ToString(), 0, Convert.ToInt32(str[0]), str.Length,0); //审核信息   最终审核通过
                    //}
                    //else 
                    //{
                    //    bll.ShenheList(NewsID.ToString(), 0, Convert.ToInt32(str[1]), str.Length,0); //审核信息  (ˇˍˇ） 向上级提交审核  
                    //}
                }
                else
                {
                    Response.Write("您不能审核此信息！");
                }

               



               //   int frequency = model.Frequency;
              //  TCS.Model.Gsp_Administrative Area = new TCS.BLL.Gsp_Administrative().GetModel(frequency); //得到区域信息
               // TCS.Model.CInfo_FlowManage fmodel = new TCS.BLL.CInfo_FlowManage().GetModel(depth);//得到对应地区审核流程

               //string[] str=fmodel.FLOWROUTE.Split(',');
               //int num=str.Length; //数组长度
               //if (num > 1)
               //{
               //    bll.ShenheList(NewsID.ToString(), 0, Convert.ToInt32(str[1]), num); //审核信息  (ˇˍˇ） 向上级提交审核
               //}
               //else 
               //{
               //    bll.ShenheList(NewsID.ToString(), 0, Convert.ToInt32(str[0]), num); //审核信息   最终审核通过
               //}
               // TCS.BLL.NewsManage.News bll = new TCS.BLL.NewsManage.News();
                dataBind(grid.CurrentPageIndex);
            }
        }
        /// <summary>
        /// 返回是第几个人审核
        /// </summary>
        /// <param name="str"></param>
        /// <param name="a"></param>
        /// <returns></returns>
        private int getIndex(string[] str, int a)
        {
            int num = str.Length;
            int result = -1;
            for (int i = 0; i < str.Length; i++)
            {
                if (int.Parse(str[i]) == a)
                {
                    result = i + 1;
                }
            }
            return result;
        }
        protected string GetNum(string n)
        {
            if ((this.grid.CurrentPageIndex + 1) > 1)
            {
                return Convert.ToString((grid.CurrentPageIndex) * grid.PageSize + (int.Parse(n) + 1));
            }
            else
            {
                return Convert.ToString(int.Parse(n) + 1);
            }
        }
        public string GetName(object a, object b)
        {
            string result = "";
            TCS.Model.Gsp_Administrative Class = new TCS.BLL.Gsp_Administrative().GetModel(int.Parse(a.ToString()));
            LTP.Accounts.Bus.User user = new LTP.Accounts.Bus.User(int.Parse(b.ToString()));
            result = Class.AddName + "-"+user.UserName;
            return result;
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            TCS.BLL.NewsManage.News bll = new TCS.BLL.NewsManage.News();
            //TCS.Model.NewsManage.News model = bll.GetModel(Id);
            Response.Redirect("release.aspx?page=1");
        }
        
        public string GetSh(object o)
        {
            if (o.ToString() == "0")
            {
                return "通过审核";
            }
            else
            {
                return "取消审核";
            }
        }
        public string GetShenhe(object o)
        {
            if (o.ToString() == "0")
            {
                return "<span style=\" color:Red\">未审核</span>";
            }
            else if (o.ToString() == "1")
            {
                return "<span style=\" color:Green\">已核中</span>";
            }
            else
            {
                return "<span style=\" color:Gray\">审核中</span>";
            }
        }

        public void NavigateToPage(object sender, CommandEventArgs e)
        {
            grid.AllowPaging = true;
            btnFirst.Enabled = true;
            btnPrev.Enabled = true;
            btnNext.Enabled = true;
            btnLast.Enabled = true;
            string pageinfo = e.CommandArgument.ToString();
            switch (pageinfo)
            {
                case "Prev":
                    PageIndex--;
                    dataBind(PageIndex);
                    break;
                case "Next":
                    PageIndex = PageIndex + 1;
                    dataBind(PageIndex);
                    break;
                case "First":
                    PageIndex = 0;
                    dataBind(PageIndex);
                    break;
                case "Last":
                    PageIndex = grid.PageCount - 1;
                    dataBind(PageIndex);
                    break;
            }
            if (PageIndex == 0)
            {
                btnFirst.Enabled = false;
                btnPrev.Enabled = false;
                if (grid.PageCount-1 == PageIndex)
                {
                    btnLast.Enabled = false;
                    btnNext.Enabled = false;
                }
            }
            else if (PageIndex == grid.PageCount - 1)
            {
                btnLast.Enabled = false;
                btnNext.Enabled = false;
            }
        }
        public string formartDate(object obj)
        {
            return obj.ToString();
        }

        /// <summary>
        /// 得到选中审核的Id
        /// </summary>
        /// zxq
        /// <returns></returns>
         private string GetSelShenHeIDlist()
        {
            string dgIDs = "";
            bool BxsChkd = false;
            foreach (DataGridItem item in grid.Items)
            {
                CheckBox deleteChkBxItem = (CheckBox)item.FindControl("SelectThis");
                      Label lblItemId = (Label)item.FindControl("lblNewsId");
                if (deleteChkBxItem.Checked)
                {
                    BxsChkd = true;
                    dgIDs +=lblItemId.Text+ ",";
                }
            }
            if (BxsChkd)
            {
                dgIDs = dgIDs.Substring(0, dgIDs.LastIndexOf(","));
            }
            return dgIDs;
        }
        /// <summary>
        /// 批量审核
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
         //protected void ImageShenhe_Click(object sender, ImageClickEventArgs e)
         //{
         //    TCS.BLL.NewsManage.News bll = new TCS.BLL.NewsManage.News();
         //    string idlist = GetSelIDlist();
         //    if (idlist.Length > 0)
         //    {
         //        //int tip = int.Parse(e.CommandArgument.ToString());
         //        //   int NewsID = (int)grid.DataKeys[e.Item.ItemIndex];
         //        //   TCS.BLL.NewsManage.News bll = new TCS.BLL.NewsManage.News();
         //        //   TCS.Model.NewsManage.News model = bll.GetModel(NewsID);


         //        bll.NoReleaseList(idlist, 0, 1);
         //    }
         //    else
         //    {
         //        Response.Redirect("releasenot.aspx");
         //    }
         //}

         protected void ImgConfirm_Click(object sender, ImageClickEventArgs e)
         {
             TCS.BLL.NewsManage.News bll = new TCS.BLL.NewsManage.News();
             string idlist = GetSelIDlist();
             if (idlist.Length > 0)
                 bll.DeleteList(idlist);
             else
             {

             }
             Response.Redirect("Releasenot.aspx");
         }
         /// <summary>
         /// 批量审核
         /// </summary>
         /// <param name="sender"></param>
         /// <param name="e"></param>
         protected void ImgCancelSh_Click(object sender, ImageClickEventArgs e)
         {
             LTP.Accounts.Bus.User currentUser = (LTP.Accounts.Bus.User)Session["UserInfo"];
             TCS.Model.Gsp_Administrative Class = new TCS.BLL.Gsp_Administrative().GetModel(int.Parse(currentUser.DepartmentID));
             int depth = Class.Depth;//流程

             TCS.BLL.NewsManage.News bll = new TCS.BLL.NewsManage.News();
             string idlist = GetSelIDlist();
             if (idlist.Length > 0)
             {   
                 TCS.Model.CInfo_FlowManage fmodel = new TCS.BLL.CInfo_FlowManage().GetModel(depth);//得到对应地区审核流程
                 string[] str = fmodel.FLOWROUTE.Split(',');
                 int num = str.Length; 
                 if (num > 1)
                 {
                     bll.ShenheList(idlist, 0, Convert.ToInt32(str[1]), num,1); //审核信息  (ˇˍˇ） 向上级提交审核
                 }
                 else
                 {
                     bll.ShenheList(idlist, 0, Convert.ToInt32(str[0]), num,1); //审核信息   最终审核通过
                 }
             }
             else
             {
                
             }
             Response.Redirect("releasenot.aspx");
         }
         public string CutString(object obj,int count)
         {
             if (obj.ToString().Length > count)
             {
                 return obj.ToString().Substring(0, count)+"...";
             }
             else
             {
                 return obj.ToString();
             }
         }
    }
}
